using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class AmbulanceContainerItem : BaseItem
	{
		private enum Anim
		{
			Static = 0,
			Close = 1,
			Open = 2
		}

		public enum State
		{
			Close = 0,
			Open = 1
		}

		private float originalDoorX;

		private float endDoorX;

		private Anim anim;

		private float animTimer;

		private float tempVal;

		private State state;

		private Transform door;

		private AmbulanceItem ambulance;

		private FrameAnimation light1Anim;

		private FrameAnimation light2Anim;

		private Transform leftTyre;

		private Transform rightTyre;

		private bool isMove;

		private bool isLampOpen;

		private float moveTimer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			door = model.Find("Door");
			originalDoorX = door.localPosition.x;
			endDoorX = originalDoorX + 3.24f;
			leftTyre = model.Find("LeftTyre");
			rightTyre = model.Find("RightTyre");
			light1Anim = model.Find("Light1").GetComponent<FrameAnimation>();
			light2Anim = model.Find("Light2").GetComponent<FrameAnimation>();
			state = (State)data.state;
		}

		public override void ResetState()
		{
			base.ResetState();
			SetState(state);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (isMove)
			{
				moveTimer += Time.deltaTime;
				leftTyre.SetLocalRotationZ(moveTimer * 180f);
				rightTyre.SetLocalRotationZ(moveTimer * 180f);
			}
			if (anim == Anim.Static)
			{
				return;
			}
			animTimer += Time.deltaTime;
			if (anim == Anim.Open)
			{
				tempVal = Mathf.Lerp(originalDoorX, endDoorX, animTimer);
				SetDoorLocalX(tempVal);
				SetCollOffsetX(tempVal);
				if (animTimer >= 1f)
				{
					anim = Anim.Static;
				}
				return;
			}
			tempVal = Mathf.Lerp(endDoorX, originalDoorX, animTimer);
			SetDoorLocalX(tempVal);
			SetCollOffsetX(tempVal);
			if (animTimer >= 1f)
			{
				anim = Anim.Static;
				AmbulanceItem ambulanceItem = GetAmbulanceItem();
				if (ambulanceItem != null)
				{
					ambulanceItem.DoorClosed();
				}
			}
		}

		private AmbulanceItem GetAmbulanceItem()
		{
			if (ambulance == null)
			{
				BaseItem itemByScriptType = Singleton<Scene>.Instance.GetItemByScriptType(153);
				if (itemByScriptType != null)
				{
					ambulance = (AmbulanceItem)itemByScriptType;
				}
			}
			return ambulance;
		}

		public void IsMove(bool val)
		{
			isMove = val;
			if (isMove)
			{
				IsLampOpen(true);
			}
			else
			{
				IsLampOpen(false);
			}
		}

		public void ChangeLampState()
		{
			IsLampOpen(!isLampOpen);
		}

		public void IsLampOpen(bool val)
		{
			if (isLampOpen != val)
			{
				isLampOpen = val;
				if (isLampOpen)
				{
					PlayDragSound();
					light1Anim.Play();
					light2Anim.Play();
				}
				else
				{
					StopDragSound();
					light1Anim.GotoFirstFrame();
					light1Anim.Stop();
					light2Anim.GotoFirstFrame();
					light2Anim.Stop();
				}
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (!isMove && anim == Anim.Static)
			{
				if (state == State.Open)
				{
					anim = Anim.Close;
					SetState(State.Close);
				}
				else if (state == State.Close)
				{
					anim = Anim.Open;
					SetState(State.Open);
				}
				Singleton<AudioManager>.Instance.PlaySound("sound_ambulance_door");
				animTimer = 0f;
			}
		}

		public void SetState(State val)
		{
			state = val;
			if (state == State.Close)
			{
				SetDoorLocalX(originalDoorX);
				SetCollOffsetX(originalDoorX);
				SetPlatformColliderEnable(false);
				SetChildsColliderEnabled(false);
			}
			else
			{
				SetDoorLocalX(endDoorX);
				SetCollOffsetX(endDoorX);
				SetPlatformColliderEnable(true);
				SetChildsColliderEnabled(true);
			}
		}

		public override bool AttachItem(BaseItem item, DepthType depthType, AttachType attachType, bool updatePosition)
		{
			bool flag = base.AttachItem(item, depthType, attachType, updatePosition);
			if (flag && state == State.Close)
			{
				item.SetAllCollidersEnabled(false);
			}
			return flag;
		}

		private void SetDoorLocalX(float x)
		{
			if (door != null)
			{
				door.SetLocalX(x);
			}
		}

		private void SetCollOffsetX(float x)
		{
			if (coll != null)
			{
				coll.offset = new Vector2(x, coll.offset.y);
			}
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
